/*
 * 文件名:Globel.hpp
 *
 * 	 功能描述:
 *  创建日期: 2010-11-27
 *  版本Ver0.01
 *  作者: huanying2000
 *  版权:copyright 2010 by the Camellia project 
 *  free software under the terms of the LGPL 2.1
 */

#ifndef GLOBEL_HPP_
#define GLOBEL_HPP_

#include <unistd.h>
#include <sys/types.h>
#include <signal.h>
#include <pthread.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <signal.h>
#include <sys/ipc.h>
#include <time.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <iconv.h>
#include <sys/prctl.h>		//Linux-specific
#include <iostream>
#include <cerrno>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <cstdlib>
#include <vector>
#include <cstring>
#include <cassert>
#include "mpg123.h"

using namespace std;

#define	GBK_UTF8_RATE	3 / 2		//汉字从GBK转换为UTF-8时储存长度的扩展比率
#define	DEBUG

const int TRACE_LEN = 128;				//音频文件路径名最大长度

typedef unsigned int uint;

struct KAudioOutput
{
	int fn;											/* filenumber */
	void *userptr;								/* driver specific pointer */
	char *device;								/* device name */
	int   flags;										/* some bits; namely headphone/speaker/line */
	long rate;										/* sample rate */
	long gain;										/* output gain */
	int channels;									/* number of channels */
	int format;									/* format flags */
	int is_open;									/* something opened? */

	/* Callbacks */
	/*int (*open)(KAudioOutput *ao);
	int (*getFormats)(KAudioOutput *ao);
	int (*write)(KAudioOutput *ao, unsigned char *, int);
	void (*flush)(KAudioOutput *ao);
	int (*close)(KAudioOutput *ao);
	int (*deinit)(KAudioOutput *ao);*/
};

struct KAudioInfo
{
	char mpegVersion[3];		/**< The MPEG version (1.0/2.0/2.5). */
	char mpegLayer[4];			/**< The MPEG Audio Layer (MP1/MP2/MP3). */
	long sampleRate; 						/**< The sampling rate in Hz. */
	char mode[16];				/**< The audio mode (Mono, Stereo, Joint-stero, Dual Channel). */
	int bitrRate;						/**< Bitrate of the frame (kbps). */
	int duration;						//音频持续时间
	double timePerFrame;		//播放每帧所需要的时间，单位秒
};


//音频文件标签，综合ID3v1，ID3v2，APEv2标签之后的标签信息
struct KAudioTag
{
	string title;           	/* 标题*/
	string artist; 			/* 作者 */
	string album;        	/* 专集 */
	char year[5]; 		/* 出品年代 */
	char trace[3];		/* 曲目号(0-99) */
	string genre; 		/* 类型 */
	string comment; 	/* 备注 */
	string innerLyric;	//内嵌歌词
};

const int AUDIO_RATE_TOLERANCE = 3;					/* 3% rate tolerance */
const int AUDIO_OUT_HEADPHONES = 0x01;
const int AUDIO_OUT_INTERNAL_SPEAKER= 0x02;
const int AUDIO_OUT_LINE_OUT = 0x04;

//字符集转换
bool stringConvert(char *outputStr, char *inputStr, const char *tocode = "UTF-8", const char *fromcode = "GBK");

ostream& operator<<(ostream &out, KAudioInfo &audioInfo);
ostream& operator<<(ostream &out, KAudioTag &audioTag);
#endif /* GLOBEL_HPP_ */
